import {
	ref
} from "vue";
import {
	onLoad,
	onShow
} from "@dcloudio/uni-app"
import dayjs from 'dayjs';
import {
	useUserStore
} from "../../stores/useUserStore";
import {
	useDishesApis
} from "../../request/dishes.api";
import {
	useRateDishesApis
} from "../../request/rate.api";

export const useController = () => {


	const apis = useDishesApis();
	const dishesList = ref([]);
	// 
	const menuDate = ref(dayjs().format('YYYY-MM-DD'));

	async function getMenuDishes() {
		const menu = await apis.getMenuByDate(menuDate.value);
		dishesList.value = menu.dishes;
	}

	function rateDishesById(score, dishId) {
		const apis = useRateDishesApis()
		return apis.rateDishes(score, dishId);
	}

	function deleteDishes(dishesId) {
		return apis.deleteDishes(dishesId);
	}

	// 检查登录
	onLoad(() => {
		const userStore = useUserStore();
		if (userStore.id === "") uni.reLaunch({
			url: "/pages/login/login"
		});
	})

	onShow(() => {
		getMenuDishes();
	});


	return {
		dishesList,
		menuDate,

		getMenuDishes,
		rateDishesById,
		deleteDishes,
	}
}